import { Component } from '@angular/core';

import { NavController, ViewController} from 'ionic-angular';

import { Storage } from '@ionic/storage';

import { Common } from "../../../providers/common";
import { Config } from "../../../providers/config";

import _ from "lodash";
import {DraftDetailService} from "./detail.service";

@Component({
	selector: 'modal-draft-companyCode',
	templateUrl: 'companyCode.modal.html',
  providers: [DraftDetailService]
})
export class DraftCompanyCodeModal{
	moa_page_name: any = "modal-draft-companyCode";
  moa_page_explain: any = "选择上市公司代码";
  // 是否可以加载更多
  isLoadMore = true;
  // 页码
  pageIndex = 1;

  dataList = [];
  dataListSource = [];

  isSelectAll = false;

  isNoMoreData = false;

  keyWord = "";

	constructor(
		public navCtrl: NavController,
    public common: Common,
    private draftDetailService: DraftDetailService,
    public viewCtrl: ViewController,
    public storage: Storage,
    public config: Config
	) {

  }

  radioSelected(item) {
    this.viewCtrl.dismiss(item);
  }

  search() {
	  this.pageIndex = 1;
	  this.loadListData(this.pageIndex, null, null);
  }

  selectAll() {
	  if (this.isSelectAll) {
      _.forEach(this.dataList, fund => {
        fund["selected"] = true;
      });
    } else {
      _.forEach(this.dataList, fund => {
        fund["selected"] = false;
      });
    }
  }

  selectItem(item) {
	  item["selected"] = !item["selected"];
  }

  /**
   * [doInfinite 加载更多]
   * @param {[type]} infiniteScroll [事件]
   */
  doInfinite(infiniteScroll) {
    if (this.pageIndex != 1) {
      if (this.isLoadMore) {
        this.loadListData(this.pageIndex, infiniteScroll, null);
      } else {
        infiniteScroll.complete();
      }
    }
  }
  /**
   * [doRefresh 下拉刷新]
   * @param {[type]} refresher [事件]
   */
  doRefresh(refresher) {
    this.pageIndex = 1;
    this.isLoadMore = true;
    this.loadListData(this.pageIndex, null, refresher);
  }

  ionViewDidEnter() {
      this.loadListData(this.pageIndex, null, null);
  }

  loadListData(page, infiniteScroll, refresher) {
    console.log(this.keyWord);
    let selectedCompanyCode = this.viewCtrl.getNavParams().get("selectedCompanyCode");
    console.log(selectedCompanyCode);
    this.draftDetailService.getCompanyCode(page, this.keyWord).subscribe(datas => {
      console.log(datas);
      if (datas) {

        // 如果加载第一页，则清空数据
        if (page == 1) {
          this.isLoadMore = true;
          this.dataList = [];
        }

        this.dataList = _.concat(this.dataList, datas);
        this.dataListSource = _.concat(this.dataList, datas);

        _.forEach(this.dataList, data => {
          /*let temp = _.find(selectedCompanyCode, sa => {
            return sa["entry"]["SECURITIESID"] == data["entry"]["SECURITIESID"];
          });*/
          if (selectedCompanyCode && selectedCompanyCode["entry"]["SECURITIESID"] == data["entry"]["SECURITIESID"]) {
            data["selected"] = true;
          } else {
            data["selected"] = false;
          }
        });

        // 增加搜索索引
        this.pageIndex = this.pageIndex + 1;
        // 如果返回数据少于请求页数，则不再加载更多
        if (datas.length < this.config.pageSize) {
          this.isLoadMore = false;
        }
      } else {
        this.dataList = [];
        this.isLoadMore = false;
      }

      // complete event
      if (infiniteScroll) {
        infiniteScroll.complete();
      }
      if (refresher) {
        refresher.complete();
      }
    });
  }

  closeModal() {
    this.viewCtrl.dismiss("close");
  }

  completeModal() {
	  let list = _.filter(this.dataList, {selected: true});
	  this.viewCtrl.dismiss(list);
  }


}

